package com.zifeng.controller;

import com.zifeng.common.R;
import com.zifeng.pojo.Block;
import com.zifeng.pojo.Order;
import com.zifeng.service.impl.BlockServiceImpl;
import com.zifeng.utils.BlockCache;
import com.zifeng.utils.PowAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 区块链区块Controller
 * 
 * @author zifeng
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/block")
public class BlockController {

    @Autowired
    private BlockCache blockCache;

    @Autowired
    private BlockServiceImpl blockServiceImpl;

    @Autowired
    private PowAlgorithm powAlgorithm;

    /**
     * 查看当前节点区块链数据
     * @return
     */
    @GetMapping("/scan")
    public R scanBlock() {
        return R.ok().put("blockchain", blockCache.getBlockChain());
    }

    /**
     * 查看当前节点区块链业务数据
     * @return
     */
    @GetMapping("/data")
    public R scanData() {
        return R.ok().put("blocktransactions", blockCache.getPackedTransactions());
    }

    /**
     * 创建创世区块
     * @return
     */
    @GetMapping("/create")
    public R createFirstBlock() {
        Block genesisBlock = blockServiceImpl.createGenesisBlock();
        return R.ok().put("genesisBlock", genesisBlock);
    }

    @PostMapping("/mine")
    public R mine(Order order) {
        Block block = powAlgorithm.mine(order);
        return R.ok().put("block", block);
    }
}
